package com.soda.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.soda.bean.Industry;
 
import com.soda.bean.News;
import com.soda.bean.Notice;
import com.soda.dao.BaseDao;
import com.soda.dao.IndustryDao;
import com.soda.util.PageInfo;
import com.soda.util.SelectIndustryInfo;
 

public class IndustryDaoImpl extends BaseDao implements IndustryDao {
	public int addIndustry(Industry ids) {
		try {
			getConnection();
			String sql = "INSERT INTO industry VALUES(default,?,?,?,NOW(),1)";
			pt = conn.prepareStatement(sql);
			pt.setString(1, ids.getIndtitle());
			pt.setInt(2, ids.getIndState());
			pt.setString(3, ids.getIndContent());
			return pt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}
		return 0;
	}

	// 全查
	public Industry showAllIndustry(int id) {
		// TODO Auto-generated method stub
		try {
			getConnection();
			String sql = "select * from Industry where ind_id=?";
			pt = conn.prepareStatement(sql);
			pt.setInt(1, id);
			res = pt.executeQuery();
			if (res.next()) {
				Industry ind = new Industry(res.getInt(1), res.getString(2),
						res.getInt(3), res.getString(4), res.getString(5),
						res.getInt(6));

				return ind;

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}

		return null;
	}

	// 条件查询

	public List<Industry> getIndustry(SelectIndustryInfo sdi, PageInfo pi) {
		
		 {
			// , PageInfo pi
			
				try {
					getConnection();
					StringBuffer sql = new StringBuffer(
							"select * from industry where ind_delete=1");
					if (sdi != null) {
						if (sdi.getIndtitle()!= null
								|| sdi.getIndtitle().length() != 0) {
							sql.append(" and ind_title like ?");
						}
						if (sdi.getIndUpdateTime() != null
								|| sdi.getIndUpdateTime().length() != 0) {
							sql.append(" and ind_updatetime like ?");
						}

						if (sdi.getIndState() !=2) {
							sql.append(" and ind_state=?");
						}

					}
					sql.append(" order by ind_id desc limit ?,? ");
					
					
				 
					
					
					
					pt = conn.prepareStatement(sql.toString());
					int x = 1;
					if (sdi != null) {
						if (sdi.getIndtitle() != null
								|| sdi.getIndtitle().length() != 0) {
							pt.setString(x, sdi.getIndtitle() + "%");
							x++;
						}
						if (sdi.getIndUpdateTime() != null
								|| sdi.getIndUpdateTime().length() != 0) {
							pt.setString(x,sdi.getIndUpdateTime() + "%");
							x++;
						}
						if (sdi.getIndState() >= 0) {
							if (sdi.getIndState() !=2) {
								pt.setInt(x, sdi.getIndState());
								x++;
							}
						}
					}
					// 分页信息
					pt.setInt(x, (pi.getPage() - 1) * pi.getPer());
					x++;
					pt.setInt(x, pi.getPer());
					List<Industry> list = new ArrayList<Industry>();
					res = pt.executeQuery();
					while (res.next()) {
						Industry ind = new Industry();
						ind.setIndId(res.getInt(1));
						ind.setIndtitle(res.getString(2));
						ind.setIndState(res.getInt(3));
						ind.setIndContent(res.getString(4));
						ind.setIndUpdateTime(res.getString(5));
						ind.setIndDelete(res.getInt(6));
						list.add(ind);
					}
					return list;
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				finally{
					close();
				}return null;
		}
		
		}

	public int getIndustryCount(SelectIndustryInfo sdi) {
		// TODO Auto-generated method stub
		try {
			getConnection();
			StringBuffer sql = new StringBuffer(
					"select count(1) from industry where ind_delete=1");
			if (sdi != null) {
				if (sdi.getIndtitle() != null) {
					sql.append(" and ind_title like ?");
				}
				if (sdi.getIndUpdateTime() != null) {
					sql.append(" and ind_updatetime like ?");
				}

				if (sdi.getIndState()!=2) {
					sql.append(" and ind_state=?");
				}

			}
			pt = conn.prepareStatement(sql.toString());
			int x = 1;
			if (sdi != null) {
				if (sdi.getIndtitle()!= null) {
					pt.setString(x, sdi.getIndtitle() + "%");
					x++;
				}
				if (sdi.getIndUpdateTime() != null) {
					pt.setString(x, sdi.getIndUpdateTime() + "%");
					x++;
				}
				if (sdi.getIndState()>= 0) {
					if (sdi.getIndState() != 2 ) {
						pt.setInt(x, sdi.getIndState());
						x++;
					}
				}
			}
			res = pt.executeQuery();
			if (res.next()) {
				int num=res.getInt(1);
				return res.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}
		return 0;
	}


	public List<Industry> selectIndustry(PageInfo pi) {
		try {
			getConnection();
			String sql = "select * from industry limit ?,?";
			pt = conn.prepareStatement(sql);
			pt.setInt(1, (pi.getPage() - 1) * pi.getPer());
			pt.setInt(2, pi.getPer());
			res = pt.executeQuery();
			List<Industry> list = new ArrayList<Industry>();
			while (res.next()) {
				Industry ids = new Industry(res.getInt(1), res.getString(2),
						res.getInt(3), res.getString(4), res.getString(5),
						res.getInt(7));
				list.add(ids);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}
		return null;
	}

	// 单删
	public int deleteById(int id) {

		try {
			getConnection();
			String sql = "UPDATE industry SET ind_delete=0 WHERE ind_id=?";
			pt = conn.prepareStatement(sql);
			pt.setInt(1, id);
			return pt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();

		}
		return 0;
	}

	// 批量删除
	public int delAllIndustry(String[] list) {

		try {
			getConnection();
			StringBuffer sql = new StringBuffer(
					"update industry set ind_delete=0 where ind_id in( ");
			for (int i = 0; i < list.length; i++) {
				sql.append("?,");
			}
			sql.deleteCharAt(sql.length() - 1);
			sql.append(")");
			 
			pt = conn.prepareStatement(sql.toString());
			int x = 1;
			for (int i = 0; i < list.length; i++) {
				pt.setInt(x, Integer.parseInt(list[i]));
				x++;
			}
			return pt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}

		return 0;
	}

	// 导出

	// 修改

	public int updateIndustry(int id, int recommend, String neirong) {

		try {
			getConnection();
			String sql = "UPDATE industry SET ind_state=?,ind_content=?,ind_updatetime=NOW()  WHERE ind_id=?";
			pt = conn.prepareStatement(sql);
			pt.setInt(1, recommend);
			pt.setString(2, neirong);
			pt.setInt(3, id);
			return pt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}

		return 0;
	}

	public List<Industry> getSelectAll() {
		try {
			getConnection();
			String sql = "SELECT * FROM information";
			pt = conn.prepareStatement(sql);
			res = pt.executeQuery();
			List<Industry> list = new ArrayList<Industry>();
			while (res.next()) {

				Industry ids = new Industry(res.getInt(1), res.getString(2),
						res.getInt(3), res.getString(4), res.getString(5),
						res.getInt(6));

				list.add(ids);
			}
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			close();

		}
		return null;
	}

	// 导出
	public List<Industry> exportIndustry(String[] count) {
		try {
			getConnection();
			StringBuffer sql = new StringBuffer(
					"select * from industry where ind_id in(");
			for (int i = 0; i < count.length; i++) {
				sql.append("?,");
			}
			sql.deleteCharAt(sql.length() - 1);
			sql.append(")");
			pt = conn.prepareStatement(sql.toString());
			int x = 1;
			for (int i = 0; i < count.length; i++) {
				pt.setInt(x, Integer.parseInt(count[i]));
				x++;
			}
			res = pt.executeQuery();
			List<Industry> list = new ArrayList<Industry>();
			while (res.next()) {
				Industry ind = new Industry(res.getInt(1), res.getString(2),
						res.getInt(3), res.getString(4), res.getString(5),
						res.getInt(6));

				list.add(ind);

			}
			return list;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close();
		}
		return null;

	} 
	//修改状态
			public int updateStateNews(int newId, int state) {
				// TODO Auto-generated method stub
				try {
					getConnection();
					String sql="update industry set ind_state=? where ind_id=?";
					pt=conn.prepareStatement(sql);
					pt.setInt(1, state);
					pt.setInt(2,newId);
					return pt.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				return 0;
			}


}
